Async Validation with deferred object#151
Open
zaalbarxx wants to merge 4 commits intorstaib:masterfrom
Open
Conversation
…hanging allowing to return jQuery deferred object like http request to allow for async check for validation
|
Thanks @zaalbarxx Sounds very useful. Let me know if I can help with anything. |
|
@t3chboy I modified your working sample slightly. I don't think I want to create another pull request based on code you wrote, but the changes that I made on your changes worked well for me so it might work for someone else as well. This is how I use it: const save = () => {
return $.ajax({url: '/api/endpoint', type: 'POST', data: this.getData() })
.done((data) => {
debugger;
})
.fail((data) => {
return false;
});
}
const onStepChanging = (event, currentIndex, newIndex) => {
$('#form-2').validate().settings.ignore = ':disabled';
// make sure frontend validation passes
if (!$('#form-2').valid()) {
return false;
}
// Only save when we move Next, return true if clicking Previous
if (currentIndex === 0 && newIndex > currentIndex) {
return save();
} else {
return true;
}
};``` |
|
As far as I could see, the problem was not with the original code but with the sample provided. You should not return the jQuery post (or get) deferred directly but create a custom one and use it inside your post or get call to resolve either with true o false. Hope this helps someone :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added possibility return deferred object instead of boolean onStepChanging allowing to return jQuery deferred object like http request to allow for async check for validation.
It has backward compatibility i.e. if boolean is returned from the callback then it will work just like it did. But if the deferred object will be returned from callback the change of state will be hold until deferred object will get resolved. ie